/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 * MIFSS  - content storage system
 * 
 *
 * @uthors: uros.kristan@gmail.com (Uroš Kristan ) Urosk.NET
 *         jernej.svigelj@gmail.com (Jernej Švigelj) 
 */
package net.monare.mifss.lib.db;

import net.monare.mifss.lib.AppContext;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.Resource;

import java.io.IOException;

public class ContentMetaDataSQLs {

    public static String TABLE_NAME_MARK = "{TABLE_NAME}";

    public ContentMetaDataSQLs() {

    }

    public String getCreateStorageTableDDL(DB_VENDOR vendor, String mainTable) throws IOException {


        Resource res = AppContext.getApplicationContext().getResource("classpath:database/" + vendor.toString().toLowerCase() + "-ddl.sql");
        String ddl = FileUtils.readFileToString(res.getFile());
        ddl = ddl.replace(TABLE_NAME_MARK, mainTable);

        return ddl;

    }

    public String getCurrentMaxIdQuery(String mainTable) {

        StringBuffer sb = new StringBuffer();

        sb.append(" SELECT MAX(ID_CONTENT) ");
        sb.append(" FROM ");
        sb.append(mainTable);

        return sb.toString();
    }

    public String getDeleteQuery(Long idContent, String mainTable) {

        StringBuffer sb = new StringBuffer();

        sb.append(" DELETE FROM " + mainTable);
        sb.append(" WHERE ");
        sb.append(" ID_CONTENT = " + idContent);

        return sb.toString();

    }

    public String getDropStorageTableDDL(DB_VENDOR vendor, String mainTable) throws IOException {


        Resource res = AppContext.getApplicationContext().getResource("classpath:database/" + vendor.toString().toLowerCase() + "-drop.sql");
        String ddl = FileUtils.readFileToString(res.getFile());
        ddl = ddl.replace(TABLE_NAME_MARK, mainTable);

        return ddl;

    }

    public String getInsertQuery(String mainTable) {

        StringBuffer sb = new StringBuffer();

        ContentMetaDataMapper dm = new ContentMetaDataMapper();

        sb.append(" INSERT INTO " + mainTable);
        sb.append("(  " + StringUtils.join(dm.getFieldList(), ",") + " ) ");
        sb.append(" VALUES  ");
        sb.append("( :" + StringUtils.join(dm.getFieldList(), ", :") + " )");

        return sb.toString();
    }

    public String getSelectAllForUserQuery(String mainTable) {

        StringBuffer sb = new StringBuffer();

        sb.append(" SELECT * ");
        sb.append(" FROM " + mainTable);
        sb.append(" WHERE ");
        sb.append(" ID_USER = :ID_USER  ORDER BY ID DESC     ");

        return sb.toString();
    }

    public String getSelectFromHashQuery(String uuidString, String mainTable) {

        StringBuffer sb = new StringBuffer();

        sb.append(" SELECT  *  ");
        sb.append(" FROM " + mainTable);
        sb.append(" WHERE ");
        sb.append(" UUID = '" + uuidString + "'");

        return sb.toString();

    }

    public String getFindAllQuery(String mainTable) {

        StringBuffer sb = new StringBuffer();

        sb.append(" SELECT * ");
        sb.append(" FROM " + mainTable);

        return sb.toString();

    }

    public String getSelectFromIdQuery(Long id, String mainTable) {

        StringBuffer sb = new StringBuffer();

        sb.append(" SELECT * ");
        sb.append(" FROM " + mainTable);
        sb.append(" WHERE ");
        sb.append(" ID_CONTENT = " + id);

        return sb.toString();

    }


    public static enum DB_VENDOR {
        MYSQL, ORACLE
    }

}
